package cz.muni.fi.pa165.web.webservice.controller;

import cz.muni.fi.pa165.api.ForestVisitService;
import cz.muni.fi.pa165.api.PickerService;
import cz.muni.fi.pa165.api.dto.ForestVisitDTO;
import cz.muni.fi.pa165.api.dto.PickerDTO;
import cz.muni.fi.pa165.webservice.PickerWebServiceAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 *
 * @author Matej Cimbora
 */
@Controller
public class JsonPickerController implements PickerWebServiceAPI {

    @Autowired
    private PickerService pickerService;
    @Autowired
    private ForestVisitService forestVisitService;

    @Override
    @RequestMapping(value = {"json/createPicker*"}, method = RequestMethod.POST)
    public @ResponseBody
    void createPicker(@RequestBody PickerDTO pickerDTO) {
        setAuth();
        try {
            pickerService.createPicker(pickerDTO);
        } catch (DataAccessException e) {
            System.out.println("chyba");
        }
    }

    @Override
    @RequestMapping(value = {"/json/getPicker*"}, method = RequestMethod.GET)
    public @ResponseBody
    PickerDTO getPicker(@RequestParam String id) {
        setAuth();
        return pickerService.getPicker(Long.valueOf(id));
    }

    @Override
    @RequestMapping(value = {"json/updatePicker*"}, method = RequestMethod.POST)
    public @ResponseBody
    void updatePicker(@RequestBody PickerDTO pickerDTO) {
        setAuth();
        pickerService.updatePicker(pickerDTO);
    }

    @Override
    @RequestMapping(value = {"json/removePicker*"}, method = RequestMethod.POST)
    public @ResponseBody
    void removePicker(@RequestBody PickerDTO pickerDTO) {
        setAuth();
        for (ForestVisitDTO fv : forestVisitService.retrieveVisitsByPicker(pickerDTO)) {
            forestVisitService.removeVisit(fv);
        }
        pickerService.deletePicker(pickerDTO);
    }

    @Override
    @RequestMapping(value = {"json/getAllPickers"}, method = RequestMethod.GET)
    public @ResponseBody
    PickerDTO[] retrieveAllPickers() {
        setAuth();
        return (PickerDTO[]) pickerService.findAllPickers().toArray(new PickerDTO[0]);
    }

    @Override
    @RequestMapping(value = {"json/getPickerByNickName*"}, method = RequestMethod.GET)
    public @ResponseBody
    PickerDTO retrievePickerByNickName(@RequestParam String name) {
        setAuth();
        return pickerService.findPickerByNickName(name);
    }

    @Override
    @RequestMapping(value = {"json/getPickersByFirstName*"}, method = RequestMethod.GET)
    public @ResponseBody
    PickerDTO[] retrievePickersByFirstName(@RequestParam String name) {
        setAuth();
        return (PickerDTO[]) pickerService.findPickersByFirstName(name).toArray(new PickerDTO[0]);
    }

    @Override
    @RequestMapping(value = {"json/getPickersByLastName*"}, method = RequestMethod.GET)
    public @ResponseBody
    PickerDTO[] retrieveAllPickersByLastName(@RequestParam String name) {
        setAuth();
        return (PickerDTO[]) pickerService.findPickersByLastName(name).toArray(new PickerDTO[0]);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public String handleException1(Exception ex) {
        return null;
    }
    
    public void setAuth() {
        Authentication auth = new UsernamePasswordAuthenticationToken("rest", "rest");
        SecurityContext context = SecurityContextHolder.getContext();
        context.setAuthentication(auth);
    }
}
